/************************************************
 * Author       :   XXY
 * Blog         :   https://www.cnblogs.com/jzyl
 * Email        :   1047185209@QQ.com
 * FileName     :   CodeMergeWindow.cs
 * CreateData   :   2023/4/28 9:30:30
 * UnityVersion :   2020.3.33f1c2
 * Description  :   描述
************************************************/

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System.Diagnostics;
using Sirenix.OdinInspector;

public class CodeMergeWindow
{
    [Title("Head")]
    [LabelWidth(150), Multiline]
    public string _Description = "将指定目录下的所有C#脚步合并为一个文件";

    [Title("代码合并")]
    [FolderPath]
    [LabelWidth(150)] public string 代码文件夹 = "";
    [FolderPath]
    [LabelWidth(150)] public string 输出路径 = "";
    [LabelWidth(150)] public string 文件名称 = "Merge.txt";
    [LabelWidth(150)] public bool 清除空行;
    [LabelWidth(150)] public bool 清除单行注释;
    [LabelWidth(150)] public bool 清除多行注释;
    public CodeMergeWindow()
    {
        代码文件夹 = Application.dataPath;
    }
    [Button("开始合并")]
    private void StartMergeCode()
    {
        if (string.IsNullOrEmpty(代码文件夹) || !Directory.Exists(代码文件夹))
        {
            ShowWarning("警告", "无效路径:" + 代码文件夹);
        }
        else if (string.IsNullOrEmpty(输出路径) || !Directory.Exists(输出路径))
        {
            ShowWarning("警告", "无效路径:" + 输出路径);
        }
        else if (string.IsNullOrEmpty(文件名称))
        {
            ShowWarning("警告", "无效名称:" + 文件名称);
        }
        else
        {
            CodeMerge();
        }
    }
    #region 提示
    private void ShowWarning(string title, string content)
    {
        EditorUtility.DisplayDialog(title, content, "确定");
    }
    #endregion
    #region 合并

    private StringBuilder contentTxt;
    private float count;
    private void CodeMerge()
    {
        contentTxt = new StringBuilder();
        count = 0;
        string[] filedir = Directory.GetFiles(代码文件夹, "*.cs", SearchOption.AllDirectories);
        Array.ForEach(filedir, (path) =>
         {
             contentTxt.AppendLine(File.ReadAllText(path, Encoding.GetEncoding("UTF-8")));
             count++;
             EditorUtility.DisplayProgressBar("合并文件", path, count / filedir.Length);
         });
        String conetnt = contentTxt.ToString();

        if (清除单行注释)
        { // 单行这是：//[^\n]*?(?=\n)
            conetnt = Regex.Replace(conetnt, "//[^\n]*?(?=\n)", "", RegexOptions.IgnoreCase, Regex.InfiniteMatchTimeout);
        }
        if (清除多行注释)
        { // 多行这是：/\*[\s\S]*?\*/
            conetnt = Regex.Replace(conetnt, "/\\*[\\s\\S]*?\\*/", "", RegexOptions.IgnoreCase, Regex.InfiniteMatchTimeout);
        }
        if (清除空行)
        {
            // 清除空行：(?<=\s)\s+?(?=\n)
            // conetnt = Regex.Replace(conetnt, "(?<=\\s)\\s+?(?=\n)", "", RegexOptions.IgnoreCase, Regex.InfiniteMatchTimeout);
            // 清除空行：^\s*\n
            conetnt = Regex.Replace(conetnt, @"^\s*\n", "", RegexOptions.IgnoreCase | RegexOptions.Multiline, Regex.InfiniteMatchTimeout);
        }

        StreamWriter sw = new StreamWriter(输出路径 + "\\" + 文件名称);
        sw.Write(conetnt);
        sw.Flush();
        sw.Close();

        EditorUtility.ClearProgressBar();
        ShowWarning("成功", "合并完成");
        OpenFolder();
    }
    #endregion
    #region 打开文件夹
    private void OpenFolder()
    {
        Process.Start("Explorer.exe", 输出路径.Replace("/", "\\"));
    }
    #endregion
}
